import requests
from lxml import etree
import os


class Spider(object):
    def start_request(self):
        # 1. 请求网站拿到HTML源代码，抽取小说名、小说链接 创建文件夹
        response = requests.get("https://www.qidian.com/all")
        xml = etree.HTML(response.text) # 整理成xml文档对象
        Bigtit_list = xml.xpath('//div[@class="book-mid-info"]/h4/a/text()') # 抽取小说名
        Bigsrc_list = xml.xpath('//div[@class="book-mid-info"]/h4/a/@href') # 抽取小说链接
        for Bigtit, Bigsrc in zip(Bigtit_list, Bigsrc_list):
            if os.path.exists(Bigtit) == False:
                os.mkdir(Bigtit)
            self.next_file(Bigtit, Bigsrc)

    def next_file(self, Bigtit, Bigsrc):
        # 2. 请求小说拿到HTML源代码，抽取章名、章链接
        response = requests.get("http:" + Bigsrc)
        xml = etree.HTML(response.text) # 整理成xml文档对象
        Littit_list = xml.xpath('//ul[@class="cf"]/li/a/text()') # 抽取章名
        Litsrc_list = xml.xpath('//ul[@class="cf"]/li/a/@href') # 抽取章链接
        for Littit, Litsrc in zip(Littit_list, Litsrc_list):
            self.finally_file(Bigtit, Littit, Litsrc)

    def finally_file(self, Bigtit, Littit, Litsrc):
        # 3. 请求文章拿到HTML源代码，抽取文章内容，保存数据
        response = requests.get("http:" + Litsrc)
        xml = etree.HTML(response.text) # 整理成xml文档对象
        content = "\n".join(xml.xpath('//div[@class="read-content j_readContent"]/p/text()'))
        fileName = Bigtit + "\\" + Littit + ".txt"
        print("正在保存小说文件：" + fileName)
        with open(fileName, "w", encoding="utf-8") as f:
            f.write(content)


spider = Spider()
spider.start_request()
